package com.yxx.community.controller;

import com.yxx.community.dto.PaginationDTO;
import com.yxx.community.mapper.QuestionMapper;
import com.yxx.community.mapper.UserMapper;
import com.yxx.community.model.QuestionExample;
import com.yxx.community.model.User;
import com.yxx.community.service.NotificationService;
import com.yxx.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 18:00 2020/1/28
 **/
@Controller
public class ProfileController {
  @Autowired
  UserMapper userMapper;
  @Autowired
  private QuestionService questionService;
  @Autowired
  private NotificationService notificationService;

  @GetMapping("/profile/{action}")
  public String profile(@PathVariable("action")String action,
                        Model model,
                        HttpServletRequest request,
                        @RequestParam(name = "page",defaultValue = "1")Integer page,
                        @RequestParam(name = "size",defaultValue = "5")Integer size){
    User user = (User) request.getSession().getAttribute("user");
    if(user==null){
      return "redirect:/";
    }
    if("questions".equals(action)){
      PaginationDTO paginationDTO = questionService.listByUserId(user.getId(), page, size);
      model.addAttribute("pagination",paginationDTO);
      model.addAttribute("section","questions");
      model.addAttribute("sectionName","我的提问");
    }else if("replies".equals(action)){
      PaginationDTO paginationDTO = notificationService.listByUserId(user.getId(), page, size);
      model.addAttribute("pagination",paginationDTO);
      model.addAttribute("section","replies");
      model.addAttribute("sectionName","最新回复");
    }
    return "profile";
  }
}
